//
//  AfterStatus.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/20.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import soapp
import AsyncDisplayKit



extension AfterStatus
{
    var limitHours:Int?{
        get{
            switch self {
            case .退货申请中,.退款申请中,.换货申请中:
                return 24
            case .退货回寄商品中,.换货商品回寄中,.卖家寄送换货商品中:
                return 24 * 15
            case .商家同意退货,.同意换货:
                return 24 * 2
            default:
                return nil
            }
        }
    }
    
    func leftTime(item:NSMutableDictionary)->String {
        if let hours = self.limitHours{
            let current_time = stampToTime(FK.remoteTimeStamp)
            let last_time = stampToTime(item["lastupdate",""])
            let fire_date = last_time.addingTimeInterval(TimeInterval(hours * 60 * 60))//超时的时间
            
            //            print("==fire_date==|\(fire_date.cn_full_date_time)")
            //            print("==last_time==|\(last_time.cn_full_date_time)")
            //            print("==current_time==|\(current_time.cn_full_date_time)")
            //
            
            let result = fire_date.timeSince(current_time)
            switch result.0{
            case .ealier:
                return  ""
            default:
                return  "剩余 \(result.1)"
            }
            
        }
        return ""
    }
}


enum AfterStatus:String{
    case 退货申请中 = "20"  //卖家24小时未处理，状态自动变为同意退货（43）
    case 退货驳回 = "21"
    case 退货申请已取消 = "22"
    case 商家同意退货 = "23"  //48小时买家不处理 转为“退款已关闭”
    case 退货回寄商品中 = "24"  //卖家15天未处理未确认收货自动转为“卖家已收货”
    case 商家已收货 = "25"
    case 退货已退款 = "26"  //退款到账
    case 退货已完成 = "27"  //退款到账（商家后台处理成为无需寄回商品，直接同意退款）
    case 退货已关闭 = "28"
    
    
    case 退款申请中 = "30" //卖家24小时未处理，状态自动变为同意退款
    case 退款驳回 = "31"
    case 申请退款已取消 = "32"
    case 同意退款 = "33"
    case 退款完成 = "37" //退款到账
    
    case 换货申请中 = "40"    //卖家24小时未处理，状态自动变为同意换货（43）
    case 换货申请被驳回 = "41"
    case 换货申请已取消 = "42"
    case 同意换货 = "43"   //48小时买家不寄回换货物品，自动转为“换货已关闭”
    case 换货商品回寄中 = "44"    //卖家15天未处理未确认收货自动转为“卖家收到换货寄回商品”（45）
    case 卖家收到换货寄回商品 = "45" //等待卖家寄货，卖家48小时未寄送物品，系统自动记为退货处理，硬性退款（48）
    case 卖家寄送换货商品中 = "46"  //买家15天未处理未确认收货自动转为“买家已确认收货”（47）
    case 买已确认收货 = "47"  //换货完成
    
    case 硬性退款 = "48"  //退款到账
    
    case none = ""
    
    var afterType:SaleAfterType{
        get{
            switch self {
            case .退款申请中,.退款驳回,.申请退款已取消,.同意退款,.退款完成:
                return SaleAfterType.returnMoney
            case .换货申请中,.换货申请被驳回,.换货申请已取消,.同意换货,.换货商品回寄中,.卖家收到换货寄回商品,.卖家寄送换货商品中,.买已确认收货,.硬性退款:
                return SaleAfterType.changeGoods
            default:
                return SaleAfterType.returnGoods
            }
        }
        
    }
    
    var baseName:String{
        switch self {
        case .退货申请中,.商家同意退货,.退货回寄商品中,.商家已收货: //退货
            return "\("退货退款".loc)  \("退货中".loc)"
        case .退货已退款,.退货已完成:
            return "\("退货退款".loc)  \("退款成功".loc)"
        case .退货已关闭,.退货驳回,.退货申请已取消:
            return "\("退货退款".loc)  \("已关闭".loc)"
        case .退款申请中,.同意退款://退款
            return "\("退款".loc)  \("退款中".loc)"
        case .退款完成:
            return "\("退货退款".loc)  \("退款成功".loc)"
        case .退款驳回,.申请退款已取消:
            return "\("退款".loc)  \("已关闭".loc)"
        case .换货申请中,.同意换货,.换货商品回寄中,.卖家收到换货寄回商品,.卖家寄送换货商品中://换货
            return "\("换货".loc)  \("换货中".loc)"
        case .硬性退款:
            return "\("换货".loc)  \("换货失败、已退款".loc)"
        case .换货申请被驳回:
            return "\("换货".loc)  \("换货驳回".loc)"
        case .换货申请已取消:
            return "\("换货".loc)  \("已取消".loc)"
        case .买已确认收货:
            return "\("换货".loc)  \("换货成功".loc)"
        case .none:
            return "售后服务".loc
        }
        
    }
    
    func logisticsCell(data:NSMutableDictionary)->[String:Any]?{
        switch self {
        case .商家已收货,.退货已退款,.退货回寄商品中:
            return ["cellID":"wuliu","title":"我的物流".loc,"__model":data,"expkey":"expresses"]
        case .换货商品回寄中,.卖家收到换货寄回商品:
            return ["cellID":"wuliu","title":"我的物流".loc,"__model":data,"expkey":"expresses"]
        case .卖家寄送换货商品中:
            return ["cellID":"wuliu","title":"换货物流".loc,"__model":data,"expkey":"rexpresses"]
        default:
            return nil
        }
        
        
    }
    
    
    
    
    
    func doAction(with btName:String,cell:JoCellNode?,at controller:UIViewController?,order:NSMutableDictionary) {
        switch btName {
        case "确认退货物流".loc:
            let ctr = ConfirmSendGoodsCtr()
            ctr.mo.model = order
            controller?.navigationController?.pushViewController(ctr, animated: true)
        case "取消申请".loc:
            let type:SaleAfterType = SaleAfterType(rawValue: order["rtype",""]) ?? .returnMoney
            
            JoAlertView(info: "提示".loc,"请确认您是否要取消[XX]申请".replaceVars(values: type.name),"取消".loc).append(title: "确定".loc, action: {
                HUD.share.show().mode(.indeterminate)

                JoTask().post.log.log_response().params(["orderid":order["orderid",""],"refundid":order["id",""]])._surl(.取消售后).json_handle { (status, msg, obj, resp) in
                    HUD.share.hide(animated: true)
                    if status.isSuccess{
                        __post(notify: Goods.Notice.changeAfterStatus)
                        "[XX]申请已取消".replaceVars(values: type.name).alert()
                        return
                    }
                    msg.alert()
                    }.run()
            }).show(at: controller)
        case "确认收货".loc:
 
            JoAlertView(info: "提示".loc,"请确认您是否已经收到换货商品".loc,"取消".loc).append(title: "确定".loc, action: {
                HUD.share.show().mode(.indeterminate)
                JoTask().post.log.log_response().params(["orderid":order["orderid",""],"refundid":order["id",""]])._surl(.收到换货商品).json_handle { (status, msg, obj, resp) in
                    HUD.share.hide(animated: true)
                    if status.isSuccess{
                        __post(notify: Goods.Notice.changeAfterStatus)
                        
                        return
                    }
                    msg.alert()
                    }.run()
            }).show(at: controller)
        default:
            break
        }
    }
    
    func noteText(obj:NSMutableDictionary)->(text:NSAttributedString,backgroundColor:UIColor,show:Bool){
        
        switch self {
        case .商家同意退货:
            let address_obj = DataNode.path(node: "__model.refundaddress", obj, value: NSMutableDictionary())
//            let province = address_obj["province",""]
//            let city = address_obj["city",""]
//            let address = address_obj["address",""]
//            let area = address_obj["area",""]
            
            let addres_str = GlobalPlaceField.displayStr(address: address_obj)
            
            let name = address_obj["name",""]
            let tel = address_obj["tel",""]
            let zipcode = address_obj["zipcode",""]
            
            var text = "\("寄送地址".loc)：\(addres_str)\n".ui.font13.cl_fff.lineSpace(5).astring
                + "\("收货人:".loc) \(name)\n".ui.bfont13.lineSpace(5).color(hex: "#fff").astring
                + "\("联系方式:".loc) \(tel)".ui.bfont13.lineSpace(5).color(hex: "#fff").astring
            
            if zipcode.len  > 0
            {
                text = text + "\n\("邮编".loc): \(zipcode)".ui.bfont13.lineSpace(5).color(hex: "#fff").astring
            }
            
            
            return (text,___shop.tint,true)
        default:
            let text = "".ui.lfont13.cl_fff.lineSpace(5).astring
            return (text,UIColor._disable,false)
        }
        
    }
    
    var barInfo:(name:String,color:UIColor,icon:String,info:String,bt:String){
        get{
            
            let able_hex = ___shop.tint
            let disable_hex:UIColor = .color(light: "#aaa", dark: "#2222")
            
            let default_icon = "wddd_pingjia"
            switch self {
            case .退货申请中:
                return ("退货申请中".loc,able_hex,default_icon,"","取消申请".loc)
            case .退货驳回:
                return ("卖家驳回退货申请".loc,able_hex,default_icon,"","")
            case .退货申请已取消:
                return ("退货已取消".loc,disable_hex,default_icon,"","")
            case .商家同意退货:
                return ("卖家同意退货".loc,able_hex,default_icon,"","确认退货物流".loc)
            case .退货回寄商品中:
                return ("正在寄送退货物流".loc,able_hex,default_icon,"","")
            case .商家已收货:
                return ("商家已收货".loc,able_hex,default_icon,"","")
            case .退货已退款:
                return ("已退款，退货完成".loc,able_hex,default_icon,"","")
            case .退货已完成:
                return ("已退款，退货完成".loc,able_hex,default_icon,"","")
            case .退货已关闭:
                return ("退货已关闭".loc,able_hex,default_icon,"","")//由于买家自己为即使处理导致的退款关闭
            case .退款申请中://退款流程中--------------------------------------------------------------------------------------
                return ("退款申请中".loc,able_hex,default_icon,"","取消申请".loc)
            case .退款驳回:
                return ("卖家驳回退款申请".loc,able_hex,default_icon,"","")
            case .申请退款已取消:
                return ("退款已取消".loc,disable_hex,default_icon,"","")
            case .同意退款:
                return ("卖家同意退款".loc,able_hex,default_icon,"","")
            case .退款完成:
                return ("退款完成".loc,able_hex,default_icon,"","")
            case .换货申请中://换货流程中--------------------------------------------------------------------------------------
                return ("换货申请中".loc,able_hex,default_icon,"","取消申请".loc)
            case .换货申请被驳回:
                return ("卖家驳回换货申请".loc,able_hex,default_icon,"","")
            case .换货申请已取消:
                return ("换货已取消".loc,disable_hex,default_icon,"","")
            case .同意换货:
                return ("卖家同意换货".loc,able_hex,default_icon,"","确认退货物流".loc)
            case .换货商品回寄中:
                return ("正在寄回换货商品".loc,able_hex,default_icon,"","")
            case .卖家收到换货寄回商品:
                return ("等待卖家寄送换货商品".loc,able_hex,default_icon,"","")
            case .卖家寄送换货商品中:
                return ("卖家已寄送换货商品".loc,able_hex,default_icon,"","确认收货".loc)
            case .买已确认收货:
                return ("换货成功".loc,able_hex,default_icon,"","")
            case .硬性退款:
                return ("换货失败（卖家已退款）".loc,able_hex,default_icon,"","")
            case .none:
                return ("",able_hex,default_icon,"","")
            }
        }
    }
        
    func statusSubtitle(obj:NSMutableDictionary) -> NSAttributedString {
        switch self {
            
        case .退货申请中://退货--------------------------------------------------
            let time = stampToTime(obj["createtime",""])["yyyy-MM-dd HH:mm"]
            return "您于[XX]发起退货申请".replaceVars(values: time).ui.font13.cl_fff.astring
        case .退货申请已取消:
            return "您已取消退货申请".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退货已关闭:
            return "由于您未在有效的时间内确认物流，退货申请已关闭".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .商家同意退货:
            return "卖家同意退货，请将购买的物品寄给卖家".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退货回寄商品中:
            return "商品已寄送，等待卖家收货".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退货已退款:
            return "规款成功，注意查收您支付账户".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退货驳回:
            return "商家驳回了您的退货申请，如有疑问请联系客服".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退货已完成:
            return "商家同意退货，注意查收您支付账户".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .退款申请中://退款--------------------------------------------------
            let time = stampToTime(obj["createtime",""])["yyyy-MM-dd HH:mm"]
            return "您于[XX]发起退款申请".replaceVars(values: time).ui.font13.cl_fff.astring
        case .退款驳回:
            return "商家驳回了您的退款申请，如有疑问请联系客服".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .申请退款已取消:
            return "您取消了退款申请".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .同意退款,.退款完成:
            return "商家同意退款，注意查收您支付账户".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .换货申请中://换货--------------------------------------------------
            let time = stampToTime(obj["createtime",""])["yyyy-MM-dd HH:mm"]
            return "您于[XX]发起换货申请".replaceVars(values: time).ui.font13.cl_fff.astring
        case .换货申请被驳回:
            return "商家驳回了您的换货申请，如有疑问请联系客服".loc.ui.font13.cl_fff.astring
        case .换货申请已取消:
            return "您已取消换货申请".loc.ui.font13.cl_fff.astring
        case .同意换货:
            return "卖家同意换货，请将购买的物品寄给卖家".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .换货商品回寄中:
            return "商品已寄送，等待卖家收货".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .卖家收到换货寄回商品:
            return "卖家收到退货商品，等待卖家发货".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .卖家寄送换货商品中:
            return "卖家已经发货，请关注物流信息".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .买已确认收货:
            return "买家收到换货物品，换货成功".loc.ui.font13.lineSpace(5).cl_fff.astring
        case .硬性退款:
            return "卖家未及时发送换货商品，系统退款至您的账户，注意查收您支付账户".loc.ui.font13.lineSpace(5).cl_fff.astring
            
        default:
            return "卖家或买家未及时处理售后操作，退货、退款会自动关闭".loc.ui.font12.color(hex: "#eee").astring
        }
    }
    
}

